﻿using System;
using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.Dust;
using Mekiasm.Ore;

namespace Mekiasm
{
    public class MekEnrich : MachineRotateAbleItem, Interface.IDynamicCraft
    {
        public static int Index;

        public Point2 CraftSize => new Point2(1, 1);

        public MekEnrich() : base("富集仓")
        {
            
            textureSlots = new int[] { 78, 93, 94, 92, 109, 77 };
        }

        public int[] FuelCraftSlotIndies => new int[0];

        public int[] CraftSlotIndies => new int[] { 0 };

        public int[] ResultSlotIndies => new int[] { 1 };

        public int[] RemainSlotIndies => new int[0];

        public int SlotsCount => 2;

        public int BlockValue => blockValue;

        public void GenerateRecipes(List<MekRecipe> mekRecipe)
        {
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("煤块").SetIngredian(0, ILibrary.GetCraftId(CoalOreBlock.Index)).SetResult(0, CoalChunkBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("铁块").SetIngredian(0, ILibrary.GetCraftId(IronOreBlock.Index)).SetResult(0, IronOreChunkBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("孔雀石块").SetIngredian(0, ILibrary.GetCraftId(CopperOreBlock.Index)).SetResult(0, MalachiteChunkBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("钻石块").SetIngredian(0, ILibrary.GetCraftId(DiamondOreBlock.Index)).SetResult(0, DiamondChunkBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("锗块").SetIngredian(0, ILibrary.GetCraftId(GermaniumOreBlock.Index)).SetResult(0, GermaniumChunkBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("硫磺块").SetIngredian(0, ILibrary.GetCraftId(SaltpeterOreBlock.Index)).SetResult(0, SaltpeterChunkBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("硝石块").SetIngredian(0, ILibrary.GetCraftId(SulphurOreBlock.Index)).SetResult(0, SulphurChunkBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("红石").SetIngredian(0, ILibrary.GetCraftId(OreRedStone.Index)).SetResult(0, RedStoneDust.Index, 12));
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("陨石粉").SetIngredian(0, ILibrary.GetCraftId(OreSkyStone.Index)).SetResult(0, SkyStoneDust.Index, 4));
        }

        public void GenerateRecipeWidget(CanvasWidget canvas, ComponentEnergyMachine energyMachine, ComponentMiner miner, ref Action Tick)
        {
            InventorySlotWidget inventory, inventory1;
            MekProgressWidget craftProgress = new MekProgressWidget(MekProgressWidget.ProgessType.down) { HorizontalAlignment = WidgetAlignment.Center, Size = new Vector2(8, 50) };
            StackPanelWidget mStackCenter = new StackPanelWidget() { Direction = LayoutDirection.Vertical, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
            LabelWidget labelWidget = new LabelWidget() { Size = new Vector2(100, 32), FontScale = 0.5f };
            inventory = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(0, 10), BevelColor = Color.Black };
            inventory.AssignInventorySlot(energyMachine, 0);
            inventory1 = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(0, 10) };
            inventory1.AssignInventorySlot(energyMachine, 1);
            CanvasWidget canvasWidget = new CanvasWidget() { Size = new Vector2(58, 50) };
            canvasWidget.Children.Add(craftProgress);
            mStackCenter.Children.Add(inventory);
            mStackCenter.Children.Add(canvasWidget);
            mStackCenter.Children.Add(inventory1);
            mStackCenter.Children.Add(labelWidget);
            canvas.AddChildren(mStackCenter);
            Tick += () => {
                craftProgress.SetProgress(energyMachine.CraftProgress);
            };
        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }
        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z,
                new List<ComponentEnergyMachine.Slot>() { new ComponentEnergyMachine.Slot(), new ComponentEnergyMachine.Slot() },
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item)
            }), new MachineAmountLimitConfig()
            {
                MaxPower = 10240,
                InputPowerLimit = 32,
                OutputPowerLimit = 32,
            });
            ElectricSystem.Add(new EnrichElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new EnrichElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ILibrary.RemoveBlockEntity(x, y, z);
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentEnrich mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentEnrich>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new EnrichWidget(mcomponent, componentMiner);
            return true;
        }
    }
}
